package com.lch.thread.vip;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author: liuchenhui
 * @create: 2019-05-13 22:57
 **/
public class SyncDmo {
    static Integer count = 0;
    static Lock lock = new ReentrantLock();

    public static void incr() {
//        synchronized (count) {
        lock.lock();
        try {
            count++;
            Thread.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
//        }
    }

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 1000; i++) {
            new Thread(SyncDmo::incr).start();
        }
        Thread.sleep(5000);
        System.out.println("result:" + count);
    }
}
